//
//  QuickLoanCarView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/12/6.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol QuickLoanCarViewDelegate {
    @objc optional func showCarSelectView(currentView:LoanScreenView) //弹出选择器
}

class QuickLoanCarView: UIView {
    
    weak var delegate : QuickLoanCarViewDelegate?
    
    let carProvinceView = LoanScreenView.init(textWidth: ScreenW/2) //车辆省份
    let carCityView = LoanScreenView.init(textWidth: ScreenW/2) //车辆城市
    let carPriceView = LoanUnitView.init(textWidth: ScreenW/2) //购车价格
    let carTimeView = LoanSelectView.init(textWidth: ScreenW/2) //购车时间
    let seatView = LoanSelectView.init(textWidth: ScreenW/2) //车辆座位
    
    var model = [String:String]() //房产相关数据

    init() {
        super.init(frame: .zero)
        
        carProvinceView.tag = 5
        carProvinceView.delegate = self
        carProvinceView.title = carProvince
        carProvinceView.placeholder = selectProvince
        self.addSubview(carProvinceView)
        
        carCityView.tag = 6
        carCityView.delegate = self
        carCityView.title = carCity
        carCityView.placeholder = selectToCity
        self.addSubview(carCityView)
        
        carPriceView.title = purchasePrice
        carPriceView.unitText = millionYuan
        carPriceView.textLabel.keyboardType = .decimalPad
        carPriceView.placeholder = selectCarPrice
        carPriceView.tag = 1
        carPriceView.delegate = self
        self.addSubview(carPriceView)
        
        carTimeView.title = purchaseTime
        carTimeView.isHide = false
        carTimeView.showSelect = false
        carTimeView.textField.keyboardType = .decimalPad
        carTimeView.placeholder = inputCarTime
        carTimeView.tag = 1
        carTimeView.delegate = self
        self.addSubview(carTimeView)
        
        seatView.tag = 2
        seatView.delegate = self
        seatView.title = hasNineSeats
        self.addSubview(seatView)
        
        carProvinceView.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.left.right.equalToSuperview()
            make.height.equalTo(40)
        }
        
        carCityView.snp.makeConstraints { (make) in
            make.top.equalTo(carProvinceView.snp.bottom).offset(10)
            make.left.right.equalToSuperview()
            make.height.equalTo(carProvinceView)
        }
        
        carPriceView.snp.makeConstraints { (make) in
            make.top.equalTo(carCityView.snp.bottom).offset(10)
            make.left.right.equalToSuperview()
            make.height.equalTo(carCityView)
        }
        
        carTimeView.snp.makeConstraints { (make) in
            make.top.equalTo(carPriceView.snp.bottom).offset(10)
            make.left.right.equalToSuperview()
            make.height.equalTo(carPriceView)
        }
        
        seatView.snp.makeConstraints { (make) in
            make.top.equalTo(carTimeView.snp.bottom).offset(10)
            make.left.right.equalToSuperview()
            make.height.equalTo(carTimeView)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension QuickLoanCarView : LoanScreenViewDelegate,LoanSelectViewDelegate,LoanUnitViewDelegate {
    
    //MARK: 选择贷款标签
    func selectLabelType(currentView: LoanScreenView) {
        
        self.delegate?.showCarSelectView!(currentView: currentView)
    }
    
    //MARK: 选择是否
    func selectLoanType(currentView: LoanSelectView) {
        
        switch currentView.tag {
        case 2:
            if currentView.isYes == "1" {
                model["car_seat"] = yes_title
            }else{
                model["car_seat"] = no_title
            }
        default:
            break
        }
    }
    
    //MARK: 购置时间
    func getInputText(currentView: LoanSelectView) {
        
        switch currentView.tag {
        case 1:
            model["car_pay_time"] = carTimeView.text
        default:
            break
        }
    }
    
    //MARK: 购置价格
    func getUnitText(currentView: LoanUnitView) {
        
        switch currentView.tag {
        case 1:
            model["car_price"] = carPriceView.text
        default:
            break
        }
    }
}
